
<?php
	include("gen.php");
	$cmd=get_datan("cmd");
	switch($cmd){
		case 1:
			//get one vaccine based on id
			get_position();
			break;
		case 2:
			//get all vaccine records and return as array
			//generate a json array/object/notation that represents all records in the vaccine table
			get_all_positions();
			break;
		case 3:
			//update_vaccine($_REQUEST['vn'],$_REQUEST['vs'],$_REQUEST['vu']);
			//update_vaccine(get_data('vn'),get_datan('vs'),get_data('url'));
			update_position();		
			break;
		case 4:
			add_position();
			break;
		case 5:
			search_position();
			break;
			//get the data from url/request and update the vaccine record
		default:
			echo "{";
			echo jsonn("result",0). ",";
			echo jsons("message","unknown command");
			echo "}";
	}
	
	
	function get_position(){
		include_once("positions.php");
		
		$id=get_datan("pid");
		$p=new positions();
		$res=$p->get_position($id);
		$row = $p->fetch($res);
		if(!$row){
			echo "{";
			echo jsonn("result",0). ",";
			echo jsons("message","position not found");
			echo "}";
			return;
		}
		
			//echo $row['firstname'];
		echo "{";
			echo jsonn("result",1) .",";
			echo '"position":{';
			echo jsonn("pid",$id) .",";
			//position name
			echo jsons("position_name",$row['position_name']) .",";
			//position created 
			echo jsons("position_created",$row['position_created']);
			echo "}";
		echo "}";
	
	}

	function get_all_positions(){ 
		include_once("positions.php");
		$p= new positions();
		$p->get_all_positions();
		$row =$p->fetch();  
		if(!$row){
			echo "{";
			echo jsonn("result",0). ",";
			echo jsons("message","position not found");
			echo "}";
			return;
		}
		echo "{";
		echo jsonn("result",1) .",";
		echo '"positions":';
		$val = Array(); 
		
		do { 
			array_push($val, $row);
			$row =$p->fetch();   
		}while ($row);	 
		print_r(json_encode($val));
		echo "}";
	}


	function update_position(){
		$id=get_datan('pid');
		$position_name=get_data('pn');
		$position_created=get_data('pc');
		if (!$id){
			echo "{";
			echo jsonn("result",0). ",";
			echo jsons("message","invalid id");
			echo "}";
			return;
		}
		include ("positions.php");
		$p= new positions();
		$valid=$p->update_positions($id,$position_name,$position_created);
		if($valid){
			echo "{";
			echo jsonn("result",1). ",";
			echo jsons("message","updated");
			echo "}";
		}else{
			echo "{";
			echo jsonn("result",0). ",";
			echo jsons("message","update failed");
			echo "}";
		}


	}

	function add_position(){
		//$id=get_datan('id');
		$position_name=get_data('pn');
		$position_created=get_data('pc');
		include ("positions.php");
		$p= new positions();
		$valid=$p->add_positions($position_name,$position_created);
		if($valid){
			echo "{";
			echo jsonn("result",1). ",";
			echo jsons("message","added");
			echo "}";
		}else{
			echo "{";
			echo jsonn("result",0). ",";
			echo jsons("message","insertion failed");
			echo "}";
		}


	}

	function search_position(){
		//$id=get_datan('id');
		$position_name=get_data('pn');
		if ($position_name == null){
			return get_all_positions();

		}
		
		include ("positions.php");
		$p= new positions();
		$valid=$p->search($position_name);
		if (!$valid){
		echo "{";
			echo jsonn("result",0). ",";
			echo jsons("message","position not found");
			echo "}";
			return;	
		}
		$row=$p->fetch();



		if(!$row){
			echo "{";
			echo jsonn("result",0). ",";
			echo jsons("message","position not found");
			echo "}";
			return;
			}

			
	
		else{
			echo "{";
			echo jsonn("result",1). ",";
			echo '"positions":';

		$val = Array(); 	
		do { 
			array_push($val, $row);
			$row =$p->fetch();   
		}while ($row);	 
		print_r(json_encode($val));
		echo "}";
	}
		


	}

?>